package usbcali.edu.co.pqrs.presentation.backingBeans;


import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.model.SelectItem;

import org.primefaces.component.commandbutton.CommandButton;
import org.primefaces.component.inputtext.InputText;
import org.primefaces.component.selectonemenu.SelectOneMenu;
import org.primefaces.context.RequestContext;
import org.primefaces.event.RowEditEvent;

import usbcali.edu.co.pqrs.exceptions.ZMessManager;
import usbcali.edu.co.pqrs.modelo.AnexosPqr;
import usbcali.edu.co.pqrs.modelo.AnxsXArea;
import usbcali.edu.co.pqrs.modelo.AreasInvolucradas;
import usbcali.edu.co.pqrs.modelo.dto.AnexosPqrDTO;
import usbcali.edu.co.pqrs.modelo.dto.AnxsXAreaDTO;
import usbcali.edu.co.pqrs.modelo.dto.AreasInvolucradasDTO;
import usbcali.edu.co.pqrs.presentation.businessDelegate.IBusinessDelegatorView;
import usbcali.edu.co.pqrs.utilities.FacesUtils;

/**
 * @author Zathura Code Generator http://code.google.com/p/zathura
 * 
 */
@ManagedBean
@ViewScoped
public class AnxsXAreaView implements Serializable {
	private static final long serialVersionUID = 1L;
//	private InputText txtEsObligatorio;
	private InputText txtIdAnxXArea;
	
	
	private SelectOneMenu txtIdAnexoPqr_AnexosPqr;
	private SelectOneMenu txtIdAreaInvolucrada_AreasInvolucradas;
	
	private String esObligatorio;
	private SelectOneMenu obligatorio;
	
	private Long idAnexoPqr_AnexosPqr;
	private Long idAreaInvolucrada_AreasInvolucradas;

	private Map<String, String> idAnexos = new HashMap<String, String>();
	private Map<String, String> idAreasInvo = new HashMap<String, String>();
	
	private CommandButton btnSave;
	private CommandButton btnModify;
	private CommandButton btnDelete;
	private CommandButton btnClear;
	
	
	private List<AnxsXAreaDTO> data;
	private AnxsXAreaDTO selectedAnxsXArea;
	private AnxsXArea entity;
	
	private boolean showDialog;
	@ManagedProperty(value = "#{BusinessDelegatorView}")
	private IBusinessDelegatorView businessDelegatorView;
	
	private SelectItem[] manufacturerOptions;

	String manufacturers[] = { "S", "N" };

	public AnxsXAreaView() {
		super();
		
		setManufacturerOptions(createFilterOptions(manufacturers));
	}
	
	private SelectItem[] createFilterOptions(String[] data) {
		SelectItem[] options = new SelectItem[data.length + 1];

		options[0] = new SelectItem("", "Seleccionar");
		for (int i = 0; i < data.length; i++) {
			options[i + 1] = new SelectItem(data[i], data[i]);
		}

		return options;
	}

	public void rowEventListener(RowEditEvent e) {
		try {
			AnxsXAreaDTO anxsXAreaDTO = (AnxsXAreaDTO) e.getObject();
//
//			if (txtEsObligatorio == null) {
//				txtEsObligatorio = new InputText();
//			}
//
//			txtEsObligatorio.setValue(anxsXAreaDTO.getEsObligatorio());

			if (txtIdAnexoPqr_AnexosPqr == null) {
				txtIdAnexoPqr_AnexosPqr = new SelectOneMenu();
			}

			txtIdAnexoPqr_AnexosPqr.setValue(anxsXAreaDTO
					.getIdAnexoPqr_AnexosPqr());

			if (txtIdAreaInvolucrada_AreasInvolucradas == null) {
				txtIdAreaInvolucrada_AreasInvolucradas = new SelectOneMenu();
			}

			txtIdAreaInvolucrada_AreasInvolucradas.setValue(anxsXAreaDTO
					.getIdAreaInvolucrada_AreasInvolucradas());

			if (txtIdAnxXArea == null) {
				txtIdAnxXArea = new InputText();
			}

			txtIdAnxXArea.setValue(anxsXAreaDTO.getIdAnxXArea());

			Long idAnxXArea = FacesUtils.checkLong(txtIdAnxXArea);
			entity = businessDelegatorView.getAnxsXArea(idAnxXArea);

			action_modify();
		} catch (Exception ex) {
		}
	}

	public String action_new() {
		action_clear();
		selectedAnxsXArea = null;
		setShowDialog(true);

		return "";
	}

	public String action_clear() {
		entity = null;
		selectedAnxsXArea = null;

//		if (txtEsObligatorio != null) {
//			txtEsObligatorio.setValue(null);
//			txtEsObligatorio.setDisabled(true);
//		}

		if (txtIdAnexoPqr_AnexosPqr != null) {
			txtIdAnexoPqr_AnexosPqr.setValue(null);
			txtIdAnexoPqr_AnexosPqr.setDisabled(true);
		}

		if (txtIdAreaInvolucrada_AreasInvolucradas != null) {
			txtIdAreaInvolucrada_AreasInvolucradas.setValue(null);
			txtIdAreaInvolucrada_AreasInvolucradas.setDisabled(true);
		}

		if (txtIdAnxXArea != null) {
			txtIdAnxXArea.setValue(null);
			txtIdAnxXArea.setDisabled(false);
		}

		if (btnSave != null) {
			btnSave.setDisabled(true);
		}

		if (btnDelete != null) {
			btnDelete.setDisabled(true);
		}

		return "";
	}

	public void listener_txtId() {
		try {
			Long idAnxXArea = FacesUtils.checkLong(txtIdAnxXArea);
			entity = (idAnxXArea != null) ? businessDelegatorView
					.getAnxsXArea(idAnxXArea) : null;
		} catch (Exception e) {
			entity = null;
		}

		if (entity == null) {
//			txtEsObligatorio.setDisabled(false);
			txtIdAnexoPqr_AnexosPqr.setDisabled(false);
			txtIdAreaInvolucrada_AreasInvolucradas.setDisabled(false);
			txtIdAnxXArea.setDisabled(false);
			btnSave.setDisabled(false);
		} else {
//			txtEsObligatorio.setValue(entity.getEsObligatorio());
//			txtEsObligatorio.setDisabled(false);
			txtIdAnexoPqr_AnexosPqr.setValue(entity.getAnexosPqr()
					.getIdAnexoPqr());
			txtIdAnexoPqr_AnexosPqr.setDisabled(false);
			txtIdAreaInvolucrada_AreasInvolucradas.setValue(entity
					.getAreasInvolucradas().getIdAreaInvolucrada());
			txtIdAreaInvolucrada_AreasInvolucradas.setDisabled(false);
			txtIdAnxXArea.setValue(entity.getIdAnxXArea());
			txtIdAnxXArea.setDisabled(true);
			btnSave.setDisabled(false);

			if (btnDelete != null) {
				btnDelete.setDisabled(false);
			}
		}
	}

	public String action_edit(ActionEvent evt) {
		selectedAnxsXArea = (AnxsXAreaDTO) (evt.getComponent().getAttributes()
				.get("selectedAnxsXArea"));
//		txtEsObligatorio.setValue(selectedAnxsXArea.getEsObligatorio());
//		txtEsObligatorio.setDisabled(false);
		txtIdAnexoPqr_AnexosPqr.setValue(selectedAnxsXArea
				.getIdAnexoPqr_AnexosPqr());
		txtIdAnexoPqr_AnexosPqr.setDisabled(false);
		txtIdAreaInvolucrada_AreasInvolucradas.setValue(selectedAnxsXArea
				.getIdAreaInvolucrada_AreasInvolucradas());
		txtIdAreaInvolucrada_AreasInvolucradas.setDisabled(false);
		txtIdAnxXArea.setValue(selectedAnxsXArea.getIdAnxXArea());
		txtIdAnxXArea.setDisabled(true);
		btnSave.setDisabled(false);
		setShowDialog(true);

		return "";
	}

	public String action_save() {
		try {
			if ((selectedAnxsXArea == null) && (entity == null)) {
				action_create();
			} else {
				action_modify();
			}

			data = null;
		} catch (Exception e) {
			FacesUtils.addErrorMessage(e.getMessage());
		}

		return "";
	}

	public String action_create() {
		try {
			entity = new AnxsXArea();

			//Long idAnxXArea = FacesUtils.checkLong(txtIdAnxXArea);

			entity.setEsObligatorio(esObligatorio);
			//entity.setIdAnxXArea(idAnxXArea);
//			entity.setAnexosPqr((FacesUtils.checkLong(txtIdAnexoPqr_AnexosPqr) != null) ? businessDelegatorView
//					.getAnexosPqr(FacesUtils.checkLong(txtIdAnexoPqr_AnexosPqr))
//					: null);
//			entity.setAreasInvolucradas((FacesUtils
//					.checkLong(txtIdAreaInvolucrada_AreasInvolucradas) != null) ? businessDelegatorView
//					.getAreasInvolucradas(FacesUtils
//							.checkLong(txtIdAreaInvolucrada_AreasInvolucradas))
//					: null);
			
			AnexosPqr entity1 = businessDelegatorView
					.getAnexosPqr(getIdAnexoPqr_AnexosPqr());
			entity.setAnexosPqr(entity1);

			AreasInvolucradas entity2 = businessDelegatorView
					.getAreasInvolucradas(getIdAreaInvolucrada_AreasInvolucradas());
			entity.setAreasInvolucradas(entity2);
			
			
			businessDelegatorView.saveAnxsXArea(entity);
			FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYSAVED);
			
			data = businessDelegatorView.getDataAnxsXArea();

			RequestContext.getCurrentInstance().update("form:listaTDo");
			
			
//			action_clear();
		} catch (Exception e) {
			entity = null;
			FacesUtils.addErrorMessage(e.getMessage());
		}

		return "";
	}

	public String action_modify() {
		try {
			if (entity == null) {
				Long idAnxXArea = new Long(selectedAnxsXArea.getIdAnxXArea());
				entity = businessDelegatorView.getAnxsXArea(idAnxXArea);
			}

//			entity.setEsObligatorio(FacesUtils.checkString(txtEsObligatorio));
			entity.setAnexosPqr((FacesUtils.checkLong(txtIdAnexoPqr_AnexosPqr) != null) ? businessDelegatorView
					.getAnexosPqr(FacesUtils.checkLong(txtIdAnexoPqr_AnexosPqr))
					: null);
			entity.setAreasInvolucradas((FacesUtils
					.checkLong(txtIdAreaInvolucrada_AreasInvolucradas) != null) ? businessDelegatorView
					.getAreasInvolucradas(FacesUtils
							.checkLong(txtIdAreaInvolucrada_AreasInvolucradas))
					: null);
			businessDelegatorView.updateAnxsXArea(entity);
			FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYMODIFIED);
		} catch (Exception e) {
			data = null;
			FacesUtils.addErrorMessage(e.getMessage());
		}

		return "";
	}

	public String action_delete_datatable(ActionEvent evt) {
		try {
			selectedAnxsXArea = (AnxsXAreaDTO) (evt.getComponent()
					.getAttributes().get("selectedAnxsXArea"));

			Long idAnxXArea = new Long(selectedAnxsXArea.getIdAnxXArea());
			entity = businessDelegatorView.getAnxsXArea(idAnxXArea);
			action_delete();
		} catch (Exception e) {
			FacesUtils.addErrorMessage(e.getMessage());
		}

		return "";
	}

	public String action_delete_master() {
		try {
			Long idAnxXArea = FacesUtils.checkLong(txtIdAnxXArea);
			entity = businessDelegatorView.getAnxsXArea(idAnxXArea);
			action_delete();
		} catch (Exception e) {
			FacesUtils.addErrorMessage(e.getMessage());
		}

		return "";
	}

	public void action_delete() throws Exception {
		try {
			businessDelegatorView.deleteAnxsXArea(entity);
			FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYDELETED);
			action_clear();
			data = null;
		} catch (Exception e) {
			throw e;
		}
	}

	public String action_closeDialog() {
		setShowDialog(false);
		action_clear();

		return "";
	}

	public String actionDeleteDataTableEditable(ActionEvent evt) {
		try {
			selectedAnxsXArea = (AnxsXAreaDTO) (evt.getComponent()
					.getAttributes().get("selectedAnxsXArea"));

			Long idAnxXArea = new Long(selectedAnxsXArea.getIdAnxXArea());
			entity = businessDelegatorView.getAnxsXArea(idAnxXArea);
			businessDelegatorView.deleteAnxsXArea(entity);
			data.remove(selectedAnxsXArea);
			FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYDELETED);
			action_clear();
		} catch (Exception e) {
			FacesUtils.addErrorMessage(e.getMessage());
		}

		return "";
	}

	public String action_modifyWitDTO(String esObligatorio, Long idAnxXArea,
			Long idAnexoPqr_AnexosPqr, Long idAreaInvolucrada_AreasInvolucradas)
			throws Exception {
		try {
			entity.setEsObligatorio(FacesUtils.checkString(esObligatorio));
			businessDelegatorView.updateAnxsXArea(entity);
			FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYMODIFIED);
		} catch (Exception e) {
			// renderManager.getOnDemandRenderer("AnxsXAreaView").requestRender();
			FacesUtils.addErrorMessage(e.getMessage());
			throw e;
		}

		return "";
	}

//	public InputText getTxtEsObligatorio() {
//		return txtEsObligatorio;
//	}
//
//	public void setTxtEsObligatorio(InputText txtEsObligatorio) {
//		this.txtEsObligatorio = txtEsObligatorio;
//	}

	public SelectOneMenu getTxtIdAnexoPqr_AnexosPqr() {
		return txtIdAnexoPqr_AnexosPqr;
	}

	public void setTxtIdAnexoPqr_AnexosPqr(SelectOneMenu txtIdAnexoPqr_AnexosPqr) {
		this.txtIdAnexoPqr_AnexosPqr = txtIdAnexoPqr_AnexosPqr;
	}

	public SelectOneMenu getTxtIdAreaInvolucrada_AreasInvolucradas() {
		return txtIdAreaInvolucrada_AreasInvolucradas;
	}

	public void setTxtIdAreaInvolucrada_AreasInvolucradas(
			SelectOneMenu txtIdAreaInvolucrada_AreasInvolucradas) {
		this.txtIdAreaInvolucrada_AreasInvolucradas = txtIdAreaInvolucrada_AreasInvolucradas;
	}

	public InputText getTxtIdAnxXArea() {
		return txtIdAnxXArea;
	}

	public void setTxtIdAnxXArea(InputText txtIdAnxXArea) {
		this.txtIdAnxXArea = txtIdAnxXArea;
	}

	public List<AnxsXAreaDTO> getData() {
		try {
			if (data == null) {
				data = businessDelegatorView.getDataAnxsXArea();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return data;
	}

	public void setData(List<AnxsXAreaDTO> anxsXAreaDTO) {
		this.data = anxsXAreaDTO;
	}

	public AnxsXAreaDTO getSelectedAnxsXArea() {
		return selectedAnxsXArea;
	}

	public void setSelectedAnxsXArea(AnxsXAreaDTO anxsXArea) {
		this.selectedAnxsXArea = anxsXArea;
	}

	public CommandButton getBtnSave() {
		return btnSave;
	}

	public void setBtnSave(CommandButton btnSave) {
		this.btnSave = btnSave;
	}

	public CommandButton getBtnModify() {
		return btnModify;
	}

	public void setBtnModify(CommandButton btnModify) {
		this.btnModify = btnModify;
	}

	public CommandButton getBtnDelete() {
		return btnDelete;
	}

	public void setBtnDelete(CommandButton btnDelete) {
		this.btnDelete = btnDelete;
	}

	public CommandButton getBtnClear() {
		return btnClear;
	}

	public void setBtnClear(CommandButton btnClear) {
		this.btnClear = btnClear;
	}

	public TimeZone getTimeZone() {
		return java.util.TimeZone.getDefault();
	}

	public IBusinessDelegatorView getBusinessDelegatorView() {
		return businessDelegatorView;
	}

	public void setBusinessDelegatorView(
			IBusinessDelegatorView businessDelegatorView) {
		this.businessDelegatorView = businessDelegatorView;
	}

	public boolean isShowDialog() {
		return showDialog;
	}

	public void setShowDialog(boolean showDialog) {
		this.showDialog = showDialog;
	}

	public Map<String, String> getIdAnexos() {
		
		try {
			List<AnexosPqrDTO> data1 = businessDelegatorView.getDataAnexosPqr();

			for (int i = 0; i < data1.size(); i++) {
				idAnexos.put(data1.get(i).getDescripcionAnexo(), data1.get(i).getIdAnexoPqr()
						+ "");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return idAnexos;
	}

	public void setIdAnexos(Map<String, String> idAnexos) {
		this.idAnexos = idAnexos;
	}

	public Map<String, String> getIdAreasInvo() {
		
		try {
			List<AreasInvolucradasDTO> data2 = businessDelegatorView.getDataAreasInvolucradas();

			for (int i = 0; i < data2.size(); i++) {
				idAreasInvo.put(data2.get(i).getNombreArea(), data2.get(i).getIdAreaInvolucrada()
						+ "");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		

		return idAreasInvo;
	}

	public void setIdAreasInvo(Map<String, String> idAreasInvo) {
		this.idAreasInvo = idAreasInvo;
	}

	public SelectItem[] getManufacturerOptions() {
		return manufacturerOptions;
	}

	public void setManufacturerOptions(SelectItem[] manufacturerOptions) {
		this.manufacturerOptions = manufacturerOptions;
	}
	
	public String getEsObligatorio() {
		return esObligatorio;
	}

	public void setEsObligatorio(String esObligatorio) {
		this.esObligatorio = esObligatorio;
	}
	
	
	public void onEdit(org.primefaces.event.RowEditEvent event) {

		
		try {

			entity = null;
			entity = businessDelegatorView
					.getAnxsXArea(((AnxsXAreaDTO) event.getObject())
							.getIdAnxXArea());
			
			
			entity.setEsObligatorio(esObligatorio);
			

			AnexosPqr entity1 = businessDelegatorView.getAnexosPqr(getIdAnexoPqr_AnexosPqr());
			entity.setAnexosPqr(entity1);

			AreasInvolucradas entity2 = businessDelegatorView
					.getAreasInvolucradas(getIdAreaInvolucrada_AreasInvolucradas());
			entity.setAreasInvolucradas(entity2);
			
			
			businessDelegatorView.updateAnxsXArea(entity);
			data = businessDelegatorView.getDataAnxsXArea();			
			RequestContext.getCurrentInstance().reset("form:listaTDo");
			FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYMODIFIED);

		} catch (Exception e) {
			e.printStackTrace();
		}

	}
	
	public void onCancel(org.primefaces.event.RowEditEvent event) {
		FacesMessage msg = new FacesMessage("Cancelled",
				((AnxsXAreaDTO) event.getObject()).getIdAnxXArea() + "");

		FacesContext.getCurrentInstance().addMessage(null, msg);
		System.out.println("Cancelado"
				+ ((AnxsXAreaDTO) event.getObject()).getIdAnxXArea());
	}

	public Long getIdAnexoPqr_AnexosPqr() {
		return idAnexoPqr_AnexosPqr;
	}

	public void setIdAnexoPqr_AnexosPqr(Long idAnexoPqr_AnexosPqr) {
		this.idAnexoPqr_AnexosPqr = idAnexoPqr_AnexosPqr;
	}

	public Long getIdAreaInvolucrada_AreasInvolucradas() {
		return idAreaInvolucrada_AreasInvolucradas;
	}

	public void setIdAreaInvolucrada_AreasInvolucradas(
			Long idAreaInvolucrada_AreasInvolucradas) {
		this.idAreaInvolucrada_AreasInvolucradas = idAreaInvolucrada_AreasInvolucradas;
	}

	public SelectOneMenu getObligatorio() {
		return obligatorio;
	}

	public void setObligatorio(SelectOneMenu obligatorio) {
		this.obligatorio = obligatorio;
	}
}
